<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回最后发生的错误</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.json-last-error-msg.html">json_last_error_msg</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="book.judy.html">Judy</a></div>
 <div class="up"><a href="ref.json.html">JSON 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.json-last-error" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">json_last_error</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">json_last_error</span> &mdash; <span class="dc-title">返回最后发生的错误</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.json-last-error-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>json_last_error</strong></span>
    ( <span class="methodparam">void</span>
   )</div>

  <p class="para rdfs-comment">
   如果有，返回 JSON 编码解码时最后发生的错误。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.json-last-error-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-last-error-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个整型（integer），这个值会是以下的常量之一：
  </p>
  <table class="doctable table">
   <caption><strong>JSON 错误码</strong></caption>
   
    <thead>
     <tr>
      <th>常量</th>
      <th>含义</th>
      <th>可用性</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td><strong><code>JSON_ERROR_NONE</code></strong></td>
      <td>没有错误发生</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_DEPTH</code></strong></td>
      <td>到达了最大堆栈深度</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td>
      <td>无效或异常的 JSON</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td>
      <td>控制字符错误，可能是编码不对</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_SYNTAX</code></strong></td>
      <td>语法错误</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_UTF8</code></strong></td>
      <td>异常的 UTF-8 字符，也许是因为不正确的编码。</td>
      <td>PHP 5.3.3</td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.json-last-error-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3670">
    <p><strong>Example #1 <span class="function"><strong>json_last_error()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;一个有效的&nbsp;json&nbsp;字符串<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">'{"Organization":&nbsp;"PHP&nbsp;Documentation&nbsp;Team"}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;一个无效的&nbsp;json&nbsp;字符串会导致一个语法错误，在这个例子里我们使用&nbsp;'&nbsp;代替了&nbsp;"&nbsp;作为引号<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"{'Organization':&nbsp;'PHP&nbsp;Documentation&nbsp;Team'}"</span><span style="color: #007700">;<br /><br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Decoding:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_NONE</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;No&nbsp;errors'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_DEPTH</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Maximum&nbsp;stack&nbsp;depth&nbsp;exceeded'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_STATE_MISMATCH</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Underflow&nbsp;or&nbsp;the&nbsp;modes&nbsp;mismatch'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_CTRL_CHAR</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Unexpected&nbsp;control&nbsp;character&nbsp;found'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_SYNTAX</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Syntax&nbsp;error,&nbsp;malformed&nbsp;JSON'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Malformed&nbsp;UTF-8&nbsp;characters,&nbsp;possibly&nbsp;incorrectly&nbsp;encoded'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Unknown&nbsp;error'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Decoding: {&quot;Organization&quot;: &quot;PHP Documentation Team&quot;} - No errors
Decoding: {&#039;Organization&#039;: &#039;PHP Documentation Team&#039;} - Syntax error, malformed JSON
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3671">
    <p><strong>Example #2 <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> 的 <span class="function"><strong>json_last_error()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;无效的&nbsp;UTF8&nbsp;序列<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\xB1\x31"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$json&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(4) &quot;null&quot;
bool(true)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.json-last-error-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.json-decode.html" class="function" rel="rdfs-seeAlso">json_decode()</a> - 对 JSON 格式的字符串进行编码</span></li>
    <li class="member"><span class="function"><a href="function.json-encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.json-last-error-msg.html">json_last_error_msg</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="book.judy.html">Judy</a></div>
 <div class="up"><a href="ref.json.html">JSON 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
